package com.example.send.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.send.bean.FileInfo;
import com.example.send.util.MathUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.LinkedBlockingDeque;

@Component
public class PanBaiduService {

    @Autowired
    private HttpAPIService httpAPIService;

    public LinkedBlockingDeque<String> block = new LinkedBlockingDeque<>();
// -------------20240705-----------
//    from_uk: 4190338887
//    msg_id: 358364346488940691
//    type: 2
//    num: 50
//    page: 1
//    fs_id: 519845166379121
//    gid: 115345714716026482
//    limit: 50
//    desc: 1
//    clienttype: 0
//    app_id: 250528
//    web: 1
//    dp-logid: 60243100326393410138


//    private static final String COOKIE = "PSTM=1637422760; BAIDUID=7E50DD966CAF508BFE49FB29C2139682:FG=1; BIDUPSID=8F9D9E7E97D4AB223E9DD0FAAECFEE88; __yjs_duid=1_14e414b94c09e82d36b552bd017dd0551637852458234; PANWEB=1; BDCLND=3VRk+YnYhVoMkVII1sLghYPXT1ZOJe/wySxqy/zKQko=; ndut_fmt=9BC795821C24EAE8FE0839CFAA0A995483F826E2F33C675E01C498E0B505BE42; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BDSFRCVID_BFESS=2y8OJeC62GqP05cHzc3ThHD3seM0T0OTH6aob-S3lxtHTRTE-uwBEG0Phx8g0KAbzcDrogKKL2OTHm_F_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_BDCLCKID_SF_BFESS=tJIe_C-atC-3Hn7gMtTJq4FDhUn2etJyaR0HXCnvWJ5TMCojKq02jttn2x-eLPv73n7e0lCMQIJcShPC-tnDqqJyhh7JBp3NQgILL43P3l02Vbn9e-t2yU_V2xbRbtRMW23roq7mWn6rsxA45J7cM4IseboJLfT-0bc4KKJxbnLWeIJIjj6jK4JKja-JqTbP; BAIDUID_BFESS=7E50DD966CAF508BFE49FB29C2139682:FG=1; BDRCVFR[eYeXh9DZh7C]=cv4bObZqw0cpvVsTA68mvqV; H_PS_PSSID=26350; delPer=0; PSINO=3; ZD_ENTRY=baidu; ab_sr=1.0.1_MWRhMWJjYWFjYThjODBmYjYyMDdkOGU3MzE5MzU1Mjc2MGMwOTIwNTMxODcyY2E1ZjhkY2EzZWU1NWJlYmM4M2MxZDVkZjNhZDFlN2Q4ZTU1OTU4NTE1NTkwMDBlMzkyMWNmNzdjZGM3ZmJhMTBiNmZjOWU1OGU1ZDUyYTQzYjJlYjkxYmY2MDA0NWIxNzZlMDdkMjIwYzgzYjFmZWE1MDk4NTgxZGQ4MGFlZGUzMmJkY2M1MmIzZjg3ZmYwYzBh; BA_HECTOR=a0852k21012g0004qc1gt0fjc0r; BDUSS=VzakF6NlppcWprWWpzSE9OYjlmWXVNZVNlbTVKSmpUdVJtczFhWkNWSEMyUGRoRVFBQUFBJCQAAAAAAAAAAAEAAAB6J6vpyfHWuNXQu7sAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMJL0GHCS9BhLT; BDUSS_BFESS=VzakF6NlppcWprWWpzSE9OYjlmWXVNZVNlbTVKSmpUdVJtczFhWkNWSEMyUGRoRVFBQUFBJCQAAAAAAAAAAAEAAAB6J6vpyfHWuNXQu7sAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMJL0GHCS9BhLT; SCRC=0f88d887c079a92293b44079deca4f33; STOKEN=016c9d7dbc132c53073bfd47f23bae9efcbfa788f4928c9599de9fc9151b4a72; Hm_lvt_7a3960b6f067eb0085b7f96ff5e660b0=1641003121,1641034894,1641035004,1641040834; Hm_lpvt_7a3960b6f067eb0085b7f96ff5e660b0=1641040834; PANPSC=8792577343599245624:CmYbeee4quX7b0ab7kJVXDuv496EomYGyZ9wemyImwYLYr2BFhFLm48eXo+5+k9fz81ttRoL0tBKQ4Dasb9/ZoQSnMlJzTiRssns6AvA/Xgh7IX89yGaj1ZmUfQoF4WTqz4hoaaKFS7R4SeWmT6baF9K9yVk1uvA4i2i1JdCg+Qoj3hBKvZymPgT7Eg6UyTb";

//    private static final String g_msgid = "1819320385022755840";
//    private static final String g_msgid = "7261695147539288538";
    private static final String g_frm = "3392479046";
//    <li class="" node-type="sharelist-item" data-id="7261695147539288538" data-frm="3392479046" data-gid="115345714716026482" data-fid="973413540739473"><div class="sharelist-item-checkbox"><a data-tag="4" class="global-icon global-icon-checkbox"></a></div><div class="sharelist-item-name"><div class="sharelist-item-title"><i class="global-icon-16 global-icon-16-dir"></i><span class="global-ellipsis sharelist-item-title-name"><a href="javascript:;" class="" data-dir="1" data-tag="0" title="会员整理" hidefocus="true">会员整理</a></span></div><div class="sharelist-item-funcs"><a class="sharelist-func-transfer global-icon-transfer" title="保存" data-tag="1" href="javascript:;" hidefocus="true">保存</a><a class="sharelist-func-download global-icon-download" title="下载" data-tag="2" href="javascript:;" hidefocus="true">下载</a></div></div><div class="sharelist-item-size global-ellipsis">-</div><div title="ti**夜曲" class="sharelist-item-sname global-ellipsis">ti**夜曲</div><div class="sharelist-item-date global-ellipsis" title="2022-01-11 11:11">01-11 11:11</div></li>


//    private static final String COOKIE = "BIDUPSID=E0E9EAD9D3FB628F28FFBE90DA2AB8C3; PSTM=1643200068; BAIDUID=E0E9EAD9D3FB628F731FCA194A7F592A:FG=1; __yjs_duid=1_e54fa430a50e36c8b070d263efebac4f1643209446139; PANWEB=1; BAIDUID_BFESS=71F4AD3BAF252DDF2104BEC27E2945E2:FG=1; BDUSS=F1ckJ5cHJiaEIzLVhQV05Lek45NnFLMDVUU2l2WUZRNm15Z2Z5Rk53NEEtVGRpRVFBQUFBJCQAAAAAAAAAAAEAAAB6J6vpyfHWuNXQu7sAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABsEGIAbBBiRW; BDUSS_BFESS=F1ckJ5cHJiaEIzLVhQV05Lek45NnFLMDVUU2l2WUZRNm15Z2Z5Rk53NEEtVGRpRVFBQUFBJCQAAAAAAAAAAAEAAAB6J6vpyfHWuNXQu7sAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABsEGIAbBBiRW; SCRC=dde67e1d30ccd1448fdb8a3f40fdf328; STOKEN=19f290b41f0d8e4752bc7a45ee0951dcf21e3c759c0c26ea5c3a94103b5a0b8a; Hm_lvt_7a3960b6f067eb0085b7f96ff5e660b0=1643215221,1645243379; Hm_lpvt_7a3960b6f067eb0085b7f96ff5e660b0=1645243394; PANPSC=2551439706294330334%3ACmYbeee4quX7b0ab7kJVXDuv496EomYGyZ9wemyImwYLYr2BFhFLm48eXo%2B5%2Bk9fz81ttRoL0tBKQ4Dasb9%2FZv%2FZ9cxipvX0ssns6AvA%2FXgh7IX89yGaj1ZmUfQoF4WTqz4hoaaKFS7R4SeWmT6baF9K9yVk1uvA4i2i1JdCg%2BQoj3hBKvZymPgT7Eg6UyTb";
//    private static final String g_msgid = "2812653581821315958";
//    private static final String g_gid = "115345714716026482";
//    String _fs_id = "290556186366864


    private static final String COOKIE = "BIDUPSID=E7CE618B912FFC5FA9CF69BC1CAC2874; PSTM=1693060645; PANWEB=1; secu=1; H_WISE_SIDS=60269_60287_60297; MCITY=-152%3A; H_WISE_SIDS_BFESS=60269_60287_60297; BDUSS=mF0MlpVa0dNa1JiNmdZWnROa3hlcWt6aTRhT2t0UWUyUk9WLWE0c0NIdkdJS3BtSVFBQUFBJCQAAAAAAAAAAAEAAAB6J6vpyfHWuNXQu7sAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMaTgmbGk4Jme; BDUSS_BFESS=mF0MlpVa0dNa1JiNmdZWnROa3hlcWt6aTRhT2t0UWUyUk9WLWE0c0NIdkdJS3BtSVFBQUFBJCQAAAAAAAAAAAEAAAB6J6vpyfHWuNXQu7sAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMaTgmbGk4Jme; STOKEN=81c5006bb1a3b536342607110cdc5ccd95c5f2a7d83a513de56c68689803ac74; BDCLND=3DfDql1PbCTxrFo053KnPOuHjIQPayPRmiXyeqdq4VE%3D; BAIDUID=7A06CC94ACA595EB7092B74B0D16DE01:FG=1; H_PS_PSSID=60272_60441; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; csrfToken=exovjdbCWDy8h_v-uXWZQxG6; newlogin=1; Hm_lvt_7a3960b6f067eb0085b7f96ff5e660b0=1717837006,1719833547,1720180381; HMACCOUNT=5776D64AA5928C2F; BAIDUID_BFESS=7A06CC94ACA595EB7092B74B0D16DE01:FG=1; delPer=0; PSINO=3; BA_HECTOR=a4a00ga5058h2g0ga4058g0hai8npq1j8fnq41u; ZFY=VA:BTtpJ2nujEKnfcAlBfZZXHNZ2WNsuEeA66E4hoR:AU:C; ZD_ENTRY=baidu; __bid_n=18c94ef009d0576e346de4; BCLID=7475227619835892650; BCLID_BFESS=7475227619835892650; BDSFRCVID=aDKOJexroG32Z-Tt7dhOheb6DyNbUdrTDYrEOwXPsp3LGJLVYdV-EG0Pts1-dEub6j30ogKKL2OTHm_F_2uxOjjg8UtVJeC6EG0Ptf8g0M5; BDSFRCVID_BFESS=aDKOJexroG32Z-Tt7dhOheb6DyNbUdrTDYrEOwXPsp3LGJLVYdV-EG0Pts1-dEub6j30ogKKL2OTHm_F_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=tJIJ_ID2JCD3H48k-4QEbbQH-UnLqb53LgOZ04n-ah05HqjzjR8b-PK8MMoatf0HW20jbUjm3UTKsq76Wh35K5tTQP6rLqFq3bv4KKJxbpbG8J5IytKaQMu3hUJiBMAHBan7Wx7IXKohJh7FM4tW3J0ZyxomtfQxtN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407145328756187%3APBu5R64BDn34Yzktx4vLVlcS2d9ns3O5g0mIZdLHpdQGbqupDlB1grA5GAXzGvBJQGuPE2OhrKwxGZiuUUeCaoyMkVJk0TTrAZ0QLnfcF09fSvclZNbrwCHshfz3IZqPVmZR9CgXhZNGMmxEFaFx4XEPb5IyECPvGYVbNx7nQJjfCPksFqz1fxzellDAqxUGcQ9vkjIQI%2B8ZhVs3HudAmN8I%2BSwWrPV%2Fm25UvfJL11k%3D";
    private static final String g_msgid = "358364346488940691";
    private static final String g_gid = "115345714716026482";
    public void send(String text) {
        if (text == null) {
            text = "";
        }
        block.offer(text);
    }

    public void start() throws Exception {
        String _name = "公考";
        int _size = 1;
        int _isDir = 1;
//        String _fs_id = "290556186366864";
        String _fs_id = "519845166379121";
        FileInfo dinfo = new FileInfo(_name, _isDir, _size, _fs_id, g_msgid, g_frm, null);

        queryDir(dinfo, "", 0);
        send("\r\n");
        send("<end>");
    }

    public void queryDir(FileInfo dir_info, String blank, int level) throws Exception {

        if (dir_info.isDir == 0) {
            send(blank + "│  " + dir_info.name + " (" + MathUtil.getSize(dir_info.size) + ")  --  " + dir_info.path + "\r\n");
            return;
        }

        level++;
        send(blank + "├─" + dir_info.name + " [文件夹" + "\r\n");

        String dir_url = "";

        int n_page = 1;
        int b_more = 1;

        int is_failed_p = 0;
        while (n_page <= 30 && b_more == 1 || is_failed_p > 0) {
            if (is_failed_p > 0) {
                System.out.println("is_failed_p:" + is_failed_p + " page: " + n_page + " dir_info.isDir: " + dir_info.isDir);
                if (is_failed_p > 2) {
                    break;
                }
            }

            dir_url = "https://pan.baidu.com/mbox/msg/shareinfo?msg_id=" + dir_info.msg_id + "&page=" + n_page + "&from_uk=" + dir_info.from_uk + "&gid=" + g_gid + "&type=2&fs_id=" + dir_info.fs_id + "&num=100";

            String resp = null;

            try {
                resp = httpAPIService.doPanBaidu(dir_url, COOKIE);
            } catch (Exception e) {
                send("errurl: " + dir_url);
            }

            if (StringUtils.isEmpty(resp)) {
                System.out.println("errurl: " + dir_url);
                is_failed_p++;
                return;
            }

            JSONObject result = JSONObject.parseObject(resp);

            if (result.getIntValue("errno") != 0) {
                System.out.println("errno:" + result.getIntValue("errno") + "url: " + dir_url);
                is_failed_p++;
                return;
            } else {
                System.out.println("yes:" + dir_url);
            }

            is_failed_p = 0;
            JSONArray file_lst = result.getJSONArray("records");
            int len = file_lst.size();
            b_more = result.getIntValue("has_more");

            for (int n = 0; n < len; n++) {
                JSONObject file = file_lst.getJSONObject(n);
                String _name = file.getString("server_filename");
                int _size = file.getIntValue("size");
                int _isDir = file.getIntValue("isdir");
                String _fs_id = file.getString("fs_id");
                FileInfo dinfo = new FileInfo(_name, _isDir, _size, _fs_id, dir_info.msg_id, dir_info.from_uk, dir_info.to_uk);
                String path = file.getString("path");
                int lastn = path.lastIndexOf("/");
                if (lastn > 0)
                    dinfo.path = path.substring(0, lastn + 1);
                else
                    dinfo.path = path;
                send(level + "");
                queryDir(dinfo, blank + "│  ", level);
            }
            n_page++;
        }
    }

}
